﻿namespace Blaze.Editor.Analysing
{
    using System;
    using System.IO;
    using Ionic.Zip;
    using UnityEditor;
    using UnityEngine;

    /// <summary>
    /// 用于分析Apk的资源占用情况。
    /// </summary>
    public static class ApkAnalysor
    {
        /// <summary>
        /// 分类器委托，传入文件路径，返回分类名称。
        /// </summary>
        public static Func<string, string> Categorizer;

//        /// <summary>
//        /// 分析指定路径下的apk包。
//        /// </summary>
//        /// <param name="path">apk包的完整路径</param>
//        public static ApkReport Analyse(string path)
//        {
//            var ret = new ApkReport();
//            var zip = new ZipFile(path);
//            foreach (ZipEntry entry in zip)
//            {
//                if (entry.IsDirectory)
//                    continue;
//                var fileInfo = new EntryInfo
//                {
//                    Category = categorize(entry.FileName),
//                    Name = entry.FileName,
//                    CompressedSize = entry.CompressedSize,
//                    UncompressedSize = entry.UncompressedSize
//                };
//                ret.Add(fileInfo);
//            }
//            return ret;
//        }

        [MenuItem("Blaze/分析/Apk", false, BlazeEditorMenu.Priority.Panels)]
        public static void OpenFileDialog()
        {
//            var lastPath = ProjectEditorPrefs.GetString("ApkAnalysor_LastPath", Application.dataPath);
//            var filePath = EditorUtility.OpenFilePanel("选择需要分析的apk文件", lastPath, "apk");
//            if (!File.Exists(filePath))
//                return;
//            ProjectEditorPrefs.SetString("ApkAnalysor_LastPath", filePath);
//            var report = Analyse(filePath);
//            var reportPath = Path.ChangeExtension(filePath, "分析报告.xlsx");
//            report.Save(reportPath);
        }

        private static string categorize(string input)
        {
            if (Categorizer == null)
                return "未分类";
            var ret = Categorizer(input);
            if (ret == null)
                return "未分类";
            return ret;
        }
    }
}